import { HttpContext } from '@adonisjs/core/http'

import User from '#models/user'
import { createShopValidator } from '#validators/shop'
import BaseController from '#common/base_controller'

export default class UserController extends BaseController {
  async list(ctx: HttpContext) {
    return this.generatePageData(ctx, User)
  }

  async create({ request }: HttpContext) {
    const data = await request.validateUsing(createShopValidator)
    return User.create(data)
  }

  async detail({ request }: HttpContext) {
    const { id } = request.qs()
    return User.findOrFail(id)
  }

  async update({ request }: HttpContext) {
    const id = request.param('id')

    const data = request.body()

    return User.query().where('id', id).update(data)
  }

  async delete({ request }: HttpContext) {
    const id = request.param('id')

    const user = await User.findOrFail(id)
    return user.delete()
  }
}
